<?php

class menuController extends Controller {
    public $initphp_list = array(
        'edit',
        'save',
        'del',
    );
    /**
     * 
     * @var menuService
     */
    private $menuService;
    public function __construct() {
        parent::__construct();
        $this->checkLogin();
        $this->menuService=InitPHP::getService("menu");
    }
    public function run() {
        $menus=$this->menuService->getMenu();
        $this->view->assign("menu_now", "menusetting");
        $this->view->assign("data", $menus);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/menu/list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "后台菜单管理-凯巴斯-管理后台");
        $this->view->display();
    }
    public function edit(){
        $id=$this->controller->get_gp("id");
        if ($id>0){
            $data=$this->menuService->getOne($id);
            $pid=$data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }else {
            $pid=$this->controller->get_gp("pid");
        }
        $parent=$this->menuService->getOne($pid);
        $this->view->assign("menu_now", "menusetting");
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/menu/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加导航-凯巴斯-管理后台");
        $this->view->display();
    }
    public function del(){
        $id=$this->controller->get_gp("id");
        $this->menuService->delete($id);
        $menu=$this->menuService->getMenu();
        $str="<?php return ".var_export($menu,true);
        file_put_contents(APP_PATH."conf/menu.php", $str.";");
        JsMessage("删除成功！","/manage/menu/run");
    }
    public function save(){
        $id=$this->controller->get_gp("id");
        $data=$this->controller->get_gp(array(
            'tag',
            'name',
            'url',
            'sort',
            'pid',
        ));
        if (!$data['pid']){
            $data['pid']=0;
        }
        $data['update_time']=time();
        $data['status']=1;
        if ($id){
            $this->menuService->update($id, $data);
        }else {
            $data['add_time']=time();
            $this->menuService->add($data);
        }
        $menu=$this->menuService->getMenu();
        $str="<?php return ".var_export($menu,true);
        file_put_contents(APP_PATH."conf/menu.php", $str.";");
        JsMessage("保存成功！",'/manage/menu/run');
    }
    /**
     * 检查登陆状态
     */
    public function checkLogin() {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}